package com.rover.inventory.service;

import com.grid.DataGrid;
import com.grid.Page;
import com.rover.inventory.entity.Product;
import com.rover.system.service.BaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 产品Service
*/

@Service
@Transactional
public class ProductService extends BaseService {
protected Logger logger = LoggerFactory.getLogger(getClass());

@Transactional
public DataGrid query(Page page) throws Exception {
DataGrid dataGrid = new DataGrid();
page.setWhere(null);
 Map<String,Object> params= new HashMap<String,Object>();
   String where="1=1";
    if(StringUtils.isNotBlank(page.getParameter("category")))
    {
        where+=" and category like:category";
        params.put("category",page.getParameter("category")+"%");
    }
    if(StringUtils.isNotBlank(page.getParameter("name"))){
        where+=" and name like:name";
        params.put("name","%"+page.getParameter("name")+"%");
    } 
    if(StringUtils.isNotBlank(page.getParameter("number"))){
        where+=" and number like:number";
        params.put("number","%"+page.getParameter("number")+"%");
    }
  
     page.setWhere(where);
    page.setWhereParams(params);
List<Product> li = dao.<Product>findPageHql(page.getHQL(), page.getWhereParams(), page.getPageNo(),
    page.getPageSize());
  
    
    dataGrid.setResult(li);
    dataGrid.setTotalCount(dao.countHql(page.getHQLTotalCount(), page.getWhereParams()));
    return dataGrid;
    }

}
